package com.neusoft.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.bo.VirtualWalletBO;
import com.neusoft.mapper.CreditMapper;
import com.neusoft.mapper.OrdersMapper;
import com.neusoft.mapper.VirtualWalletMapper;
import com.neusoft.mapper.VirtualWalletTransactionMapper;
import com.neusoft.po.Orders;
import com.neusoft.po.VirtualWallet;
import com.neusoft.po.VirtualWalletTransaction;
import com.neusoft.service.VirtualWalletService;
import com.neusoft.util.CommonUtil;

@Service
public class VirtualWalletServiceImpl implements VirtualWalletService{
	
	@Autowired
	VirtualWalletMapper virtualWalletMapper;
	@Autowired
	VirtualWalletTransactionMapper virtualWalletTransactionMapper;
	@Autowired
	OrdersMapper ordersMapper;
	@Autowired
	CreditMapper creditMapper;
	
	//充血模型service中不涉及业务操作，应封装在BO中（本项目BO与PO不区分）
	//数据库CRUD操作将数据持久化不算业务逻辑部分
	//service仅仅负责业务逻辑和持久化等操作的流程调度
	//本方法只有对数据库的CRUD操作，因此方法内与BO无关
	public int saveWallet(String userId) {
		//创建BO对象
		VirtualWallet wallet = new VirtualWallet();
		//BO转PO(这里直接使用BO)
		//持久化对象
		wallet.setUserId(userId);
		wallet.setCreateTime(CommonUtil.getCurrentDate());
		return virtualWalletMapper.saveWallet(wallet);
		//return 0;
	}
	
//	public int saveTransaction(VirtualWalletTransaction v) {
//		v.setTime(CommonUtil.getCurrentDate());
//		return virtualWalletTransactionMapper.saveTransaction(v);
//	}
	
	//充值
	@Transactional
	public int recharge(Integer walletId, String userId, Double addAmount) {
		//1.取得BO对象
		VirtualWalletBO virtualWalletBO = virtualWalletMapper.getWallet(walletId, userId);
		//2.BO处理业务,并处理业务中涉及的持久对象
		VirtualWalletTransaction transaction = virtualWalletBO.recharge(addAmount);
		virtualWalletTransactionMapper.saveTransaction(transaction);
		//3.BO转PO（直接用BO也行）
		VirtualWallet virtualWallet = new VirtualWallet();
		virtualWallet.setWalletId(walletId);
		virtualWallet.setUserId(userId);
		virtualWallet.setCreateTime(virtualWalletBO.getCreateTime());
		virtualWallet.setBalance(virtualWalletBO.getBalance());
		//4.PO持久化
		return virtualWalletMapper.updateWallet(virtualWallet);
	}
	
	//支付
	public int payment(Integer orderId, Integer walletId, String userId, Double paymentAmount) {
		//1.取得BO对象
		VirtualWalletBO virtualWalletBO = virtualWalletMapper.getWallet(walletId, userId);
		//2.处理业务
		VirtualWalletTransaction transaction = virtualWalletBO.payment(paymentAmount);
		virtualWalletTransactionMapper.saveTransaction(transaction);
		//修改订单状态
		ordersMapper.payOrders(orderId);
		//3.BO->PO
		VirtualWallet virtualWallet = new VirtualWallet();
		virtualWallet.setWalletId(walletId);
		virtualWallet.setUserId(userId);
		virtualWallet.setCreateTime(virtualWalletBO.getCreateTime());
		virtualWallet.setBalance(virtualWalletBO.getBalance());
		//4.
		return virtualWalletMapper.updateWallet(virtualWallet);
	}
	
	//提现
	public int withdraw(Integer walletId, String userId, Double withdrawAmount) {
		//1.取得BO对象
		VirtualWalletBO virtualWalletBO = virtualWalletMapper.getWallet(walletId, userId);
		//2.处理业务
		VirtualWalletTransaction transaction = virtualWalletBO.withdraw(withdrawAmount);
		virtualWalletTransactionMapper.saveTransaction(transaction);
		//3.BO->PO
		VirtualWallet virtualWallet = new VirtualWallet();
		virtualWallet.setWalletId(walletId);
		virtualWallet.setUserId(userId);
		virtualWallet.setCreateTime(virtualWalletBO.getCreateTime());
		virtualWallet.setBalance(virtualWalletBO.getBalance());
		//4.
		return virtualWalletMapper.updateWallet(virtualWallet);
	}
	
	//查询余额
	//这里没用BO进行操作，对于本项目这样更方便
	public Double getBalanceByUserId(String userId) {
		return virtualWalletMapper.getBalanceByUserId(userId);
	}
	
	//查询交易流水
	@Override
	public List<VirtualWalletTransaction> getTransaction(Integer walletId) {
		return virtualWalletTransactionMapper.getTransaction(walletId);
	}

	//获取钱包
	@Override
	public VirtualWallet getWalletByUserId(String userId) {
		return virtualWalletMapper.getWalletByUserId(userId);
		
	}

}
